//
//  ListNodes.swift
//  soapp_Example
//
//  Created by zhu xietong on 2018/2/13.
//  Copyright © 2018年 CocoaPods. All rights reserved.
//

import Foundation
import AsyncDisplayKit
import Eelay
open class JoTouchNode: MoDisplayNode {
    

    public let button = ASButtonNode()
    public required init() {
        super.init()
    }
    open override func addLayoutRules() {
        button.addTarget(self, action: #selector(buttonAction), forControlEvents: .touchUpInside)

    }
    
    @objc open func buttonAction() {
        active(ID: "JoTouchNode_action")
    }
//    override open func layoutSpecThatFits(_ constrainedSize: ASSizeRange) -> ASLayoutSpec {
//        let lay = fastLayoutSpec(_spec: specGetter())
//
//        return ASOverlayLayoutSpec(child: lay, overlay: __button)
//    }
    
}
extension Array where Self.Element == MoDisplayNode {
    public func reuse(_ index:Int)->Element{
        if count > index
        {
            let item = self[index]
            return item
        }
        return Element()
    }
}
open class JoRuleNode<Kit:MoDisplayNode>: MoDisplayNode,EeActionDelegate{
    open func active(onwer: Any, actionID: String, data: NSMutableDictionary) {
        self.delegate?.active(onwer: onwer, actionID: actionID, data: data)
    }
        
    open override func mo_will_display() {
        for node in nodes {
            node.mo_will_display()
        }
    }
    open override func mo_end_display() {
        for node in nodes {
            node.mo_end_display()
        }
    }
    
    override open func addLayoutRules() {
        
    }
    
    public var nodes:[Kit] = [Kit]()
    public var nodePath:String = "list"
    public var space:CGFloat = 10
    public var isReUse:Bool = false
    public var oldNodes:[Kit] = [Kit]()
    
    public var containerConfig:(ASStackLayoutSpec)->Void = {_ in}
    

    
    open override func specGetter() -> _Spec {
        let childs = self.nodes.map { (n) -> _Spec in
            n.Spec
        }
        let spec:_Spec = .stack(["h|\(self.space)","id|list",_shrink(1)], childs: childs)    
        return spec
    }
    
    public var initKits:(_:[Kit])->Void = {_ in}

    public var didLoadKits:(_:[Kit])->Void = {_ in}
    
    open override func mo_model_reload() {
       
        if isReUse{
            self.oldNodes = nodes
        }else{
            self.oldNodes = nodes

        }
        var subnodes = [Kit]()
        
        let list = mo.model.get(node: nodePath, defaultV: NSMutableArray())
        if let obj = list.lastObject as? NSMutableDictionary{
            obj["__last__"] = "YES"
        }
        
        for (i,_) in Each<NSMutableDictionary>(list).enumerated() {
            if i < oldNodes.count{
                let subkit = oldNodes[i]
                subkit.delegate = self
                subkit.mo.controller = mo.controller
                subnodes.append(subkit)

            }else{
                let subkit = Kit()
                subkit.delegate = self
                subkit.mo.controller = mo.controller
                subnodes.append(subkit)
            }
        }
        initKits(subnodes)

        for (i,dict) in Each<NSMutableDictionary>(list).enumerated() {
            if i < subnodes.count{
                let subkit = subnodes[i]
                dict["__index"] = "\(i)"
                subkit.nodeIndex = i
                subkit.mo.model = dict
            }
        }

        self.nodes = subnodes
        self.invalidateCalculatedLayout()
        self.setNeedsLayout()
        didLoadKits(subnodes)
        
    }
    

    open override func layoutSpecThatFits(_ constrainedSize: ASSizeRange) -> ASLayoutSpec {
        let spec = fastLayoutSpec(_spec: specGetter())
        if let stack = spec as? ASStackLayoutSpec
        {
            containerConfig(stack)
        }
        return spec
    }

}
